#!/usr/bin/env python
"""
CSPBuilder plugin system

Copyright (c) 2008 Rune M. Friborg, runef@diku.dk.
See LICENSE.txt for licensing details (GPLv2).
"""

from extensions import cspxml

location_SHEET   = 1001
location_PROCESS = 1002
location_CHANNEL = 1003
location_CPOINT  = 1004
location_PROCESS_CODE = 1005
location_PROCESS_LINK = 1006

location_INSERTCOMP_TOP = 1007
location_INSERTCOMP_BOTTOM = 1008 # not used yet
location_INSERTCOMP_LEFT = 1009 # not used yet
location_INSERTCOMP_RIGHT = 1010 # not used yet

node_PROCESS_CONTAINER = 2001
node_PROCESS = 2002
node_CODE = 2003
node_PROCESS_LINK = 2004
node_CHANNEL = 2005
#node_CONNECTION = 2006
node_CHANNELEND = 2007
node_CPOINT = 2008

pathlist_COMPONENTS = 3001


GUI_ID_NEXT_NUMBER = 8001

class PluginManager():
    
    def __init__(self, identifier):
        self.plugins = []
        self.identifier = identifier

    def getID(self):
        global GUI_ID_NEXT_NUMBER
        GUI_ID_NEXT_NUMBER += 1
        return GUI_ID_NEXT_NUMBER
        
    def load(self):
        pass
        
    def reload(self):
        self.plugins = []
        self.load()
        
    def addPlugin(self, plugin):
        plugin.index = len(self.plugins)
        self.plugins.append(plugin)

    # Not implememted yet
    def removePlugin(self, index):
        pass


class Plugin():
    
    def __init__(self, manager, name):
        self.manager = manager
        self.name = name
        self.callbacks = {}
        self.index = None        

    def addListCallback(self, key, item):
        if not self.callbacks.has_key(key):
            self.callbacks[key] = []
        self.callbacks[key].append(item)
        

    def getListCallback(self, key):
        if self.callbacks.has_key(key):
            return self.callbacks[key]
        return []

    def addDictCallback(self, key, def_id, item):
        if not self.callbacks.has_key(key):
            self.callbacks[key] = {}
        D = self.callbacks[key]
        if not D.has_key(def_id):
            D[def_id] = []
        D[def_id].append(item)

    def getDictCallback(self, key, def_id):
        if self.callbacks.has_key(key):
            if self.callbacks[key].has_key(def_id):
                return self.callbacks[key][def_id]
        return []

    

# Utilities

import sys

def print_r(item, l=0):
    if (type(item) == type([])):
        #sys.stdout.write('  '*l + '-' + '\n')
        sys.stdout.write('[')
        for i in item:
            print_r(i, l+1)
        sys.stdout.write(']')
    elif (type(item) == type({})):
        #sys.stdout.write('  '*l + '-' + '\n')
        sys.stdout.write('{')
        for i in item:
            sys.stdout.write('  '*l + i+':')
            print_r(item[i], l+1)
        sys.stdout.write('}')
    else:
        #sys.stdout.write('  '*l)
        print ' '+str(item)

